/**
 * @(#)Range.java Created: 2009-10-29
 *
 * Copyright 2009 Zeus Medicine Group (ZS)
 * All rights reserved.
 *
 */

package nc.ui.zzjs.pub.excel;

import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

/**Class - Range.java
*
* @version 1.0
* @date 2009-10-29
* @author smile
*
**/
public class Range {
	private final int xlEdgeLeft = 7;
	private final int xlEdgeTop = 8;
	private final int xlEdgeBottom = 9;
	private final int xlEdgeRight = 10;
	
	private final int xlInsideVertical = 11;
	private final int xlInsideHorizontal = 12;
	
	private final int xlCenter = -4108;
	private final int xlLeft = -4131;
	private final int xlRight = -4152;
	
	private Dispatch range = null;
	
	public Range(Dispatch range) {
		this.range = range;
	}
	
	public Dispatch getDispatch() {
		return range;
	}
	
	public void setVerticalAlignmentCenter() {
		Dispatch.put(range, "VerticalAlignment", new Variant(xlCenter));
	}
	
	public void setHorizontalAlignmentLeft() {
		Dispatch.put(range, "HorizontalAlignment", new Variant(xlLeft));
	}
	
	public void setHorizontalAlignmentRight() {
		Dispatch.put(range, "HorizontalAlignment", new Variant(xlRight));
	}
	
	public void setHorizontalAlignmentCenter() {
		Dispatch.put(range, "HorizontalAlignment", new Variant(xlCenter));
	}
	
	public void clear() {
		Dispatch.call(range, "Clear");
	}
	
	public void copy() {
		Dispatch.call(range, "Copy");
	}
	
	public void cut() {
		Dispatch.call(range, "Cut");
	}
	
	public void delete() {
		Dispatch.call(range, "Delete");
	}
	
	public void merge() {
		Dispatch.call(range, "Merge");
	}
	
	public int getCount() {
		Variant v = Dispatch.get(range, "Count");
		return v.getInt();
	}
	
	public void select() {
		try {
			Dispatch.call(range, "Select");
		}
		catch(Exception e) { }
	}
	
	public void unmerge() {
		Dispatch.call(range, "UnMerge");
	}
	
	public Variant getValue() {
		Variant v = Dispatch.get(range, "Value");
		return v;
	}
	
	public XlsFont getFont() {
		Variant v = Dispatch.get(range, "Font");
		return new XlsFont(v.toDispatch());
	}
	
	public void clearComment() {
		Dispatch.call(range, "ClearComments");
	}
	
	public void setComment(String v) {
		Dispatch.call(range, "ClearComments");
		Dispatch.call(range, "AddComment");
		Variant c = Dispatch.get(range, "Comment");
		Dispatch.put(c.toDispatch(), "Visible", false);
		Dispatch.call(c.toDispatch(), "Text", v);
	}
	
	public void setValue(Variant v) {
		Dispatch.put(range, "Value", v);
	}
	
	public void setValue(Object v) {
		Dispatch.put(range, "Value", new Variant(v));
	}
	
	public void setNumberFormatLocal(String format) {
		Dispatch.put(range, "NumberFormatLocal", new Variant(format));
	}
	
	public void setRowHeight(double height) {
		Dispatch.put(range, "RowHeight", new Variant(height));
	}
	
	public void setInteriorColorIndex(int colorIndex) {
		Variant interior = Dispatch.get(range, "Interior");
		Dispatch.put(interior.toDispatch(), "ColorIndex", new Variant(colorIndex));
	}
	
	public void setInteriorPattern(int patternIndex) {
		Variant interior = Dispatch.get(range, "Interior");
		Dispatch.put(interior.toDispatch(), "Pattern", new Variant(patternIndex));
	}
	
	private final int xlGray8 = 18;
	public void setInteriorPatternGray8() {
		Variant interior = Dispatch.get(range, "Interior");
		Dispatch.put(interior.toDispatch(), "Pattern", new Variant(xlGray8));
	}
	
	public Border getTopBorder() {
		Variant v1 = Dispatch.get(range, "Borders");
		Variant v2 = Dispatch.invoke(v1.toDispatch(), "Item", Dispatch.Get,
			new Object[]{xlEdgeTop}, new int[1]);
		return new Border(v2.toDispatch());
	}
	
	public Border getLeftBorder() {
		Variant v1 = Dispatch.get(range, "Borders");
		Variant v2 = Dispatch.invoke(v1.toDispatch(), "Item", Dispatch.Get,
			new Object[]{xlEdgeLeft}, new int[1]);
		return new Border(v2.toDispatch());
	}
	
	public Border getBottomBorder() {
		Variant v1 = Dispatch.get(range, "Borders");
		Variant v2 = Dispatch.invoke(v1.toDispatch(), "Item", Dispatch.Get,
			new Object[]{xlEdgeBottom}, new int[1]);
		return new Border(v2.toDispatch());
	}
	
	public Border getRightBorder() {
		Variant v1 = Dispatch.get(range, "Borders");
		Variant v2 = Dispatch.invoke(v1.toDispatch(), "Item", Dispatch.Get,
			new Object[]{xlEdgeRight}, new int[1]);
		return new Border(v2.toDispatch());
	}
	
	public Border getInsideVerticalBorder() {
		Variant v1 = Dispatch.get(range, "Borders");
		Variant v2 = Dispatch.invoke(v1.toDispatch(), "Item", Dispatch.Get,
			new Object[]{xlInsideVertical}, new int[1]);
		return new Border(v2.toDispatch());
	}
	
	public Border getInsideHorizontal() {
		Variant v1 = Dispatch.get(range, "Borders");
		Variant v2 = Dispatch.invoke(v1.toDispatch(), "Item", Dispatch.Get,
			new Object[]{xlInsideHorizontal}, new int[1]);
		return new Border(v2.toDispatch());
	}
}
